
package com.tyy.springbootmysql.util;

import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;


/**
 * 线程池工具类
 * 
 * @Date: 2016年8月26日 下午2:57:13
 * @author: xuefeng
 */
public class ThreadPoolUtil {

	public static ThreadPoolTaskExecutor poolTaskExecutor = getThreadPool();

	public static ThreadPoolTaskExecutor aiPoolTaskExecutor = getAiThreadPool();

	private static ThreadPoolTaskExecutor getAiThreadPool() {
		if (null == aiPoolTaskExecutor) {
			aiPoolTaskExecutor = new ThreadPoolTaskExecutor();
			// 线程池所使用的缓冲队列
			aiPoolTaskExecutor.setQueueCapacity(10);
			// 线程池维护线程的最少数量
			aiPoolTaskExecutor.setCorePoolSize(1);
			// 线程池维护线程的最大数量
			aiPoolTaskExecutor.setMaxPoolSize(10);
			// 线程池维护线程所允许的空闲时间
			aiPoolTaskExecutor.setKeepAliveSeconds(30000);
//			poolTaskExecutor.setWaitForTasksToCompleteOnShutdown(true);
//			poolTaskExecutor.setAwaitTerminationSeconds(60);
			aiPoolTaskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
			aiPoolTaskExecutor.initialize();
		}
		return aiPoolTaskExecutor;
	}

	private static ThreadPoolTaskExecutor getThreadPool() {
		if (null == poolTaskExecutor) {
			poolTaskExecutor = new ThreadPoolTaskExecutor();
			// 线程池所使用的缓冲队列
			poolTaskExecutor.setQueueCapacity(100000);
			// 线程池维护线程的最少数量
			poolTaskExecutor.setCorePoolSize(5);
			// 线程池维护线程的最大数量
			poolTaskExecutor.setMaxPoolSize(10);
			// 线程池维护线程所允许的空闲时间
			poolTaskExecutor.setKeepAliveSeconds(30000);
//			poolTaskExecutor.setWaitForTasksToCompleteOnShutdown(true);
//			poolTaskExecutor.setAwaitTerminationSeconds(60);
//			poolTaskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
			poolTaskExecutor.initialize();
		}
		return poolTaskExecutor;
	}

	// /**
	//  *
	//  * @param crmStudentOld
	//  * @param crmResourceOld
	//  * @param userInfoOld
	//  * @param messageContentOld
	//  * @param mobilePhoneOld
	//  * @author: xuefeng
	//  * @email: xuefeng6@xdf.cn
	//  * @创建日期: 2019年7月24日
	//  * @功能说明: 公开课发送短信
	//  */
	// public static void publicClassSendMsg(CrmStudent crmStudentOld, CrmResource crmResourceOld, UserInfo userInfoOld, String messageContentOld,
	// 									  String mobilePhoneOld) {
	// 	if (StringUtils.isNotBlank(mobilePhoneOld)) {
	// 		final CrmStudent crmStudent = crmStudentOld;
	// 		final CrmResource crmResource = crmResourceOld;
	// 		final UserInfo userInfo = userInfoOld;
	// 		final String messageContent = messageContentOld;
	// 		final String mobilePhone = mobilePhoneOld;
	// 		PublicClassSendMsgThread thread = new PublicClassSendMsgThread(crmStudent, crmResource, userInfo, messageContent,
	// 				mobilePhone);
	// 		poolTaskExecutor.execute(thread);
	// 	}
	//
	// }
	//
    // /**
    //  * 创建线程池
    //  * corePoolSize： 线程池核心线程数
    //  * maximumPoolSize：线程池最大数
    //  * keepAliveTime： 空闲线程存活时间
    //  * unit： 时间单位
    //  * workQueue： 线程池所使用的缓冲队列
    //  */
    // public static ExecutorService executorService = new ThreadPoolExecutor(10, 500, 1, TimeUnit.SECONDS, new LinkedBlockingDeque());
	//
	//
    // /**
    //  *
    //  * @param tasks
    //  * @param <T>
    //  * @return
    //  */
    // public static <T> List<T> addTask(List<Callable<T>> tasks) {
    //     List<Future<T>> futureList = null;
    //     List<T> results = null;
    //     try {
    //         futureList = executorService.invokeAll(tasks);
    //         results = new ArrayList<>();
    //         for (Future<T> future : futureList) {
    //             results.add(future.get());
    //         }
    //     } catch (InterruptedException | ExecutionException e) {
    //         e.printStackTrace();
    //     }
    //     return results;
    // }

}
